<?php
namespace app\data\controller\api;
require_once './vendor/autoload.php';
// require_once ("../extend/page.php");
// require_once 'ROOT_PATH'.'/vendor/autoload.php';
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Sms\V20210111\SmsClient;
use TencentCloud\Sms\V20210111\Models\SendSmsRequest;
class Sendsms{

    public function Sms($phone,$SecretId,$SecretKey){
        try {
            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            $cred = new Credential($SecretId, $SecretKey);
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            $httpProfile = new HttpProfile();
            $httpProfile->setEndpoint("sms.tencentcloudapi.com");

            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            $clientProfile = new ClientProfile();
            $clientProfile->setHttpProfile($httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            $client = new SmsClient($cred, "ap-beijing", $clientProfile);

            // 实例化一个请求对象,每个接口都会对应一个request对象
            $req = new SendSmsRequest();

            // {
            //     "PhoneNumberSet": [
            //         "+8618511122266",
            //         "+8618511122233"
            //     ],
            //     "SmsSdkAppId": "1400006666",
            //     "SignName": "腾讯云",
            //     "TemplateId": "1234",
            //     "TemplateParamSet": [
            //         "12345"
            //     ],
            //     "SessionContext": "test"
            // }
            $code = '';
            for ($i = 0; $i < 10; $i++) {
                $code .= rand(0,9);
            }

            $params = array(
                "PhoneNumberSet"=>[$phone],
                "SmsSdkAppId"=>'1400715981',
                "SignName"=>'吉林省汪清县公证处',
                "TemplateId"=>'1581960',
                "TemplateParamSet" => [substr($code, 2, 2).substr($code, 6, 2)],
                // "TemplateParamSet" => [$code],
                "SessionContext"=>'',
            );
            $req->fromJsonString(json_encode($params));
            $code =  $params['TemplateParamSet'][0];
            // 返回的resp是一个SendSmsResponse的实例，与请求对象对应
            $resp = $client->SendSms($req);
            // return $resp->toJsonString();
            $str = $resp->toJsonString();

            $rt = json_decode($str,true);
            if (isset($rt['SendStatusSet'][0]['Message'])&&$rt['SendStatusSet'][0]['Message']=='send success'){
                return $code;
            }else{
                return 0;
            }
            // 输出json格式的字符串回包

        }
        catch(TencentCloudSDKException $e) {
            echo $e;
        }
    }

    public function Sms_status($phone,$SecretId,$SecretKey,$templateid,$data){
        try {
            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            $cred = new Credential($SecretId, $SecretKey);
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            $httpProfile = new HttpProfile();
            $httpProfile->setEndpoint("sms.tencentcloudapi.com");

            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            $clientProfile = new ClientProfile();
            $clientProfile->setHttpProfile($httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            $client = new SmsClient($cred, "ap-beijing", $clientProfile);

            // 实例化一个请求对象,每个接口都会对应一个request对象
            $req = new SendSmsRequest();

            $params = array(
                "PhoneNumberSet"=>[$phone],
                "SmsSdkAppId"=>'1400715981',
                "SignName"=>'吉林省汪清县公证处',
                "TemplateId"=>$templateid,
                "TemplateParamSet"=>$data,
                "SessionContext"=>'',
            );
            $req->fromJsonString(json_encode($params));

            // 返回的resp是一个SendSmsResponse的实例，与请求对象对应
            $resp = $client->SendSms($req);
            // return $resp->toJsonString();
            $str = $resp->toJsonString();

            $rt = json_decode($str,true);
            if (isset($rt['SendStatusSet'][0]['Message'])&&$rt['SendStatusSet'][0]['Message']=='send success'){
                return 1;
            }else{
                return 0;
            }
            // 输出json格式的字符串回包

        }
        catch(TencentCloudSDKException $e) {
            echo $e;
        }
    }

}
